package com.example.base.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.base.R;
import com.example.base.adapter.SerachViewAdapter;
import com.example.base.bean.Question;
import com.example.base.bean.QuestionThree;
import com.example.base.sqllite.MySQLite;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
  private ListView listView;
  private SerachViewAdapter serachViewAdapter;
  private final List<QuestionThree> questions = new ArrayList<>();
  private ImageButton imageButton;
  private final String TAG = "SearchActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_serach);
    initView();
    initListener();
    initViewPager();
    Log.d(TAG, "onCreate()");
  }

  private void initViewPager() {
    String search = getIntent().getStringExtra("result");
    Gson gson = new Gson();
    Question question = gson.fromJson(search, Question.class);
    serachViewAdapter = new SerachViewAdapter(this, questions);
    listView.setAdapter(serachViewAdapter);
    refreshListView(question);
  }

  private void initView() {
    listView = findViewById(R.id.list_ser_result);
    imageButton = findViewById(R.id.img_serach_back);
  }

  private void refreshListView(Question question) {
    List<QuestionThree> data = question.getData().getDatas();
    serachViewAdapter.setData(data);
    serachViewAdapter.notifyDataSetChanged();
  }

  private void initListener() {
    ListViewListener();
    imageButton.setOnClickListener(v -> finish());

  }

  private void ListViewListener() {
    listView.setOnItemClickListener((parent, view, position, id) -> {
      List<QuestionThree> dataList = serachViewAdapter.getDataList();
      SQLiteInsert(position, dataList);
      Intent intent = new Intent(SearchActivity.this, WebViewActivity.class);
      intent.putExtra("Url", dataList.get(position).getLink());
      startActivity(intent);
    });
  }

  private void SQLiteInsert(int position, List<QuestionThree> dataList) {
    ContentValues cv = new ContentValues();
    cv.put("title", String.valueOf(Html.fromHtml(dataList.get(position).getChapterName())));
    cv.put("text", String.valueOf(Html.fromHtml(dataList.get(position).getTitle())));
    cv.put("link", dataList.get(position).getLink());
    @SuppressLint("SimpleDateFormat") SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    cv.put("time", s.format(new Date()));
    MySQLite user = new MySQLite(SearchActivity.this, "user", null, 1);
    SQLiteDatabase db = user.getWritableDatabase();
    long row = db.insert("history", null, cv);
    Log.d(TAG, "row = " + row);
    db.close();
  }
}